From f76db9c790b3cec9079e1472fa95106ac8d025ae Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Wed, 14 Feb 2018 12:27:19 -0500 Subject: [PATCH] When -v is passed with --list, display path to custom commands --- src/bin/cargo.rs | 21 ++++++++++++++++----- tests/cargo.rs | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index fa2ba69ea..1f282f82d 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -180,7 +180,15 @@ fn execute(flags: Flags, config: &mut Config) -> CliResult { if flags.flag_list { println!("Installed Commands:"); for command in list_commands(config) { - println!(" {}", command); + let (command, path) = command; + if flags.flag_verbose > 0 { + match path { + Some(p) => println!(" {:<20} {}", command, p), + None => println!(" {:<20}", command), + } + } else { + println!(" {}", command); + } } return Ok(()); } @@ -301,7 +309,7 @@ fn find_closest(config: &Config, cmd: &str) -> Option { // Only consider candidates with a lev_distance of 3 or less so we don't // suggest out-of-the-blue options. let mut filtered = cmds.iter() - .map(|c| (lev_distance(c, cmd), c)) + .map(|&(ref c, _)| (lev_distance(c, cmd), c)) .filter(|&(d, _)| d < 4) .collect::>(); filtered.sort_by(|a, b| a.0.cmp(&b.0)); @@ -347,7 +355,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C } /// List all runnable commands -fn list_commands(config: &Config) -> BTreeSet { +fn list_commands(config: &Config) -> BTreeSet<(String, Option)> { let prefix = "cargo-"; let suffix = env::consts::EXE_SUFFIX; let mut commands = BTreeSet::new(); @@ -367,13 +375,16 @@ fn list_commands(config: &Config) -> BTreeSet { } if is_executable(entry.path()) { let end = filename.len() - suffix.len(); - commands.insert(filename[prefix.len()..end].to_string()); + commands.insert( + (filename[prefix.len()..end].to_string(), + Some(path.display().to_string())) + ); } } } macro_rules! add_cmd { - ($cmd:ident) => ({ commands.insert(stringify!($cmd).replace("_", "-")); }) + ($cmd:ident) => ({ commands.insert((stringify!($cmd).replace("_", "-"), None)); }) } each_subcommand!(add_cmd); commands diff --git a/tests/cargo.rs b/tests/cargo.rs index 55de7b9c7..35e22c641 100644 --- a/tests/cargo.rs +++ b/tests/cargo.rs @@ -74,7 +74,7 @@ fn list_command_looks_at_path() { .env("PATH", &path); let output = output.exec_with_output().unwrap(); let output = str::from_utf8(&output.stdout).unwrap(); - assert!(output.contains("\n 1\n"), "missing 1: {}", output); + assert!(output.contains("\n 1 "), "missing 1: {}", output); } // windows and symlinks don't currently agree that well @@ -95,7 +95,7 @@ fn list_command_resolves_symlinks() { .env("PATH", &path); let output = output.exec_with_output().unwrap(); let output = str::from_utf8(&output.stdout).unwrap(); - assert!(output.contains("\n 2\n"), "missing 2: {}", output); + assert!(output.contains("\n 2 "), "missing 2: {}", output); } #[test] -- 2.30.2